package com.example.dreamlist2;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import cn.bmob.v3.Bmob;
import androidx.appcompat.app.AppCompatActivity;

//起始页
//要求必须经过登陆后/注册后才可以进入
//点击登录-进入登陆界面
//点击注册-进入注册界面

public class IndexActivity extends AppCompatActivity {
    private Button btn_log, btn_reg;
    StateInfo state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        state = (StateInfo)this.getApplication();
        Bmob.initialize(this, "82a2b595e45dc7eabfeae49089ab8b23");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index);
        btn_log = findViewById(R.id.btn_log);
        btn_reg = findViewById(R.id.btn_reg);

        btn_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userLogin();
            }
        });

        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userRegister();
            }
        });
    }

    //理论上来说不可能出现已经登陆的情况
    private void userLogin(){
        if(state == null){
            Toast.makeText(IndexActivity.this,"出错……",Toast.LENGTH_LONG);
            return;
        }
        if(state.getLogin()){
            Toast.makeText(IndexActivity.this,"您已登录，即将跳转，请稍候……",Toast.LENGTH_LONG);
            Utils.launch(IndexActivity.this, BaseActivity.class, null, null);
        }
        else{
            Utils.launch(IndexActivity.this, LoginActivity.class, null, null);
        }
    }

    private void userRegister(){
        if(state.getLogin()){
            Toast.makeText(IndexActivity.this,"您已登录，即将跳转，请稍候……",Toast.LENGTH_LONG);
            Utils.launch(IndexActivity.this, BaseActivity.class, null, null);
        }
        else{
            Utils.launch(IndexActivity.this, RegisterActivity.class, null, null);
        }
    }
}